module Texture {
	export class DynamicLoadGroup extends egret.DisplayObjectContainer{
		private _txInfo:egret.TextField;

		/**
		 * 构造函数
		 */
		public constructor() {
			super();
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
		}

		/**
		 * 添加到舞台监听器
		 */
		private onAddToStage(event: egret.Event) {
			this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchHandler,this);
			/// 提示信息
			this._txInfo = new egret.TextField;
			this._txInfo.size = 28;
			this._txInfo.textAlign = egret.HorizontalAlign.CENTER;
			this._txInfo.textColor = 0x843900;
			this._txInfo.lineSpacing = 6;
			this._txInfo.multiline = true;
            this._txInfo.text = "点击屏幕添加精灵";
            this._txInfo.width = this.stage.stageWidth;
			this._txInfo.y = 120;
            this.addChild(this._txInfo);
            //背景
            var bg: egret.Shape = new egret.Shape;
            bg.graphics.beginFill(0x338888, 1.0);
            bg.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
            bg.graphics.endFill();
            this.addChildAt(bg,0);
		}

		/**
		 * 触摸事件
		 */
        private touchHandler(evt: egret.TouchEvent): void {
            var texture: egret.Texture = RES.getRes("hero_png");
            RES.hasRes("hero_png");
            if (texture == null) {
                egret.log("资源组未加载，开始加载资源组！");
                //加载配置文件
                RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
                RES.addEventListener(RES.ResourceEvent.CONFIG_LOAD_ERROR, this.onConfigError, this);
                RES.loadConfig("resource/default.res.json", "resource/");
            } else {
                egret.log("资源组已加载，直接生成精灵！");
                this.createSprite("hero_png");
            }
        }

        /**
        *配置文件加载完成事件
        */
        private onConfigComplete(evt: RES.ResourceEvent): void {
            RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
            RES.removeEventListener(RES.ResourceEvent.CONFIG_LOAD_ERROR, this.onConfigError, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onGroupComplete, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onGroupError, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onGroupProgress, this);
            RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemError, this);
            egret.log("配置文件加载完成，开始加载资源组！");
            egret.log("创建前：" + RES.getGroupByName("sprites").length);
            RES.createGroup("sprites", ["hero_png", "dirt_jpg", "egret_icon2_png"]);
            egret.log("创建后：" + RES.getGroupByName("sprites").length);
            RES.loadGroup("sprites");
        }

        /**
        *配置文件加载错误
        */
        private onConfigError(evt: RES.ResourceEvent): void {
            egret.error("配置文件加载错误！");
            RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
            RES.removeEventListener(RES.ResourceEvent.CONFIG_LOAD_ERROR, this.onConfigError, this);
        }

        /**
        *资源组加载完成
        */
        private onGroupComplete(evt: RES.ResourceEvent): void {
            egret.log("资源组加载完成！");
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onGroupComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onGroupError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onGroupProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemError, this);
            this.createSprite("hero_png");
        }

        /**
        *资源组加载中
        */
        private onGroupProgress(evt: RES.ResourceEvent): void {
            egret.log("资源加载中：", evt.itemsLoaded, evt.itemsTotal);
            egret.log(evt.resItem.url, evt.resItem.name);      
            if (evt.resItem.name == "hero_png") {
                this.createSprite("hero_png");
            }    
        }

        /**
        *资源组加载失败
        */
        private onGroupError(evt: RES.ResourceEvent): void {
            egret.error("资源组加载失败！");
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onGroupComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onGroupError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onGroupProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemError, this);
        }

        /**
        *资源项加载失败
        */
        private onItemError(evt: RES.ResourceEvent): void {
            egret.error("资源：" + evt.resItem.url + "加载失败！");
        }

        /**
        *创建精灵
        */
        private createSprite(name:string): void {
            var sp: egret.Bitmap = new egret.Bitmap;
            sp.texture = RES.getRes(name);
            sp.x = this.stage.stageWidth * Math.random();
            sp.y = this.stage.stageHeight * Math.random();
            sp.anchorOffsetX = sp.width / 2;
            sp.anchorOffsetY = sp.height / 2;
            this.addChild(sp);
        }
	}
}